/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.search;

import gov.fema.hazus.bdc.client.common.entities.AbstractClickListener;
import gov.fema.hazus.bdc.client.controller.Controller;
import gov.fema.hazus.bdc.client.ui.BuildingList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Search extends Composite {

	private static Search singleton;
	public static Search get()
	{
		if (singleton != null) {
			return singleton;
		}
		return (singleton = new Search());
	}
	
	private HorizontalPanel main;
	private SearchConstants constants;
	private TextBox searchBox;
	private Button searchButton;
	private HTML message;
	
	private BuildingSearchClickListener buildingSearchClickListener;
	private BuildingSearchTextBoxKeyListener buildingSearchTextBoxKeyListener;
	
	public Search()
	{
		
		// constants
		constants = (SearchConstants)GWT.create(SearchConstants.class);
		
		// listeners
		buildingSearchClickListener = new BuildingSearchClickListener();
		buildingSearchTextBoxKeyListener = new BuildingSearchTextBoxKeyListener();
		
		// setup 
		main = new HorizontalPanel();
		
		// logo
		Image logo = new Image("images/incast/hazus.logo.long.jpg");
		logo.setHeight("54");
		logo.setWidth("400");
		logo.setStyleName("logo");
		main.add(logo);
		
		// search
		searchBox = new TextBox();
		searchButton = new Button(constants.search());	
		searchButton.setStyleName("incast-command-button");
		
		// add the click listener
		searchButton.addClickListener(buildingSearchClickListener);
		
		// add a listener for the ENTER key in the text box
		searchBox.addKeyboardListener(buildingSearchTextBoxKeyListener);
		
		// add to main
		main.add(searchBox);
		main.add(searchButton);
		
		// fixup alignment
		main.setCellVerticalAlignment(searchBox, HasVerticalAlignment.ALIGN_MIDDLE);
		main.setCellVerticalAlignment(searchButton, HasVerticalAlignment.ALIGN_MIDDLE);
		
		// add message
		message = new HTML();
		message.setWordWrap(false);
		main.add(message);
		main.setCellVerticalAlignment(message, HasVerticalAlignment.ALIGN_MIDDLE);
		
		initWidget(main);
	}

	public void setMessage(HTML message) {
		this.message = message;
	}

	public String getQuery() {
		return searchBox.getText();
	}
	
	public void clearQuery()
	{
		this.searchBox.setText("");
	}
	
	public class BuildingSearchTextBoxKeyListener extends KeyboardListenerAdapter
	{
		public void onKeyDown(Widget sender, char keyCode, int modifiers)
		{
			GWT.log("keyCode=" + (int) keyCode, null);
			GWT.log("handling BuildingSearchTextBoxKeyListener", null);
			if( (int) keyCode == KEY_ENTER)
			{
				GWT.log("Sending to controller via ENTER key", null);
				BuildingList.get().resetAndExecuteSearch(Search.get().getQuery());
				//Controller.get().buildingListResetAndExecuteSearch();
			}
		}
	}
	
	public class BuildingSearchClickListener extends AbstractClickListener
	{

		public void handleClick(Widget sender) {
			GWT.log("Sending to controller via BUTTON click", null);
			BuildingList.get().resetAndExecuteSearch(Search.get().getQuery());
			//Controller.get().buildingListResetAndExecuteSearch();
		}		
	}	
}
